package com.ayf.payment.game.api.controller;

import com.ayf.payment.game.api.dto.MerchantDTO;
import com.ayf.payment.game.api.dto.PageDTO;
import com.ayf.payment.game.api.dto.criteria.MerchantBankCriteria;
import com.ayf.payment.game.api.entity.MerchantBank;
import com.ayf.payment.game.api.service.MerchantBankService;
import com.ayf.payment.game.api.type.CommonConstants;
import com.ayf.payment.game.api.type.Status;
import com.ayf.payment.game.api.utils.CommonResult;
import com.ayf.payment.game.api.utils.SensitiveDataUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.swwx.charm.commons.lang.utils.BeanConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zh-liang
 * @Date : 2020-03-07 18:42
 */
@RestController
@RequestMapping("/v/bank")
public class MerchantBankController {

    @Autowired
    private MerchantBankService merchantBankService;

    /**
     * 查询结算银行卡信息
     *
     * @param merchantDTO
     * @return
     */
    @PostMapping("/selectOne")
    public CommonResult<MerchantBank> selectOne(MerchantDTO merchantDTO) {
        MerchantBank merchantBank = new MerchantBank();
        merchantBank.setStatus(Status.ENABLE);
        merchantBank.setMerchantId(merchantDTO.getId());
        MerchantBank bank = merchantBankService.selectOne(merchantBank);
        if (bank != null) {
            bank.setAccountName(SensitiveDataUtil.accountNameDesensitization(bank.getAccountName()));
            if (bank.getSettleType() == 0) {
                bank.setCardNo(SensitiveDataUtil.cardNoDesensitization(bank.getCardNo()));
            } else if (bank.getSettleType() == 1) {
                bank.setCardNo(SensitiveDataUtil.alipayAccountNoDesensitization(bank.getCardNo()));
            }
        }
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "查询结算银行卡信息成功", bank);
    }

    /**
     * 增加新的结算银行卡
     *
     * @param merchantBankCriteria
     * @param merchantDTO
     * @return
     */
    @PostMapping("/insert")
    public CommonResult insert(@RequestBody MerchantBankCriteria merchantBankCriteria, MerchantDTO merchantDTO) {
        merchantBankCriteria.setMerchantId(merchantDTO.getId());
        merchantBankCriteria.setMerchantStatus(merchantDTO.getStatus().name());
        if (merchantBankService.insert(merchantBankCriteria)==1) {
            return new CommonResult(CommonConstants.RETURN_SUCCESS, "结算信息变更成功");
        }else {
            return new CommonResult(CommonConstants.RETURN_SUCCESS, "结算信息变更申请成功，请等待运营人员审核");
        }
    }

    /**
     * 查询结算银行卡变更记录
     *
     * @param merchantBankCriteria
     * @param merchantDTO
     * @return
     */
    @PostMapping("/list")
    public CommonResult<PageDTO> list(@RequestBody MerchantBankCriteria merchantBankCriteria, MerchantDTO merchantDTO) {
        merchantBankCriteria.setMerchantId(merchantDTO.getId());
        List<String> list=new ArrayList<>();
        list.add(Status.PROHIBIT.name());
        list.add(Status.AUDITED.name());
        list.add(Status.AUDITFAIL.name());
        list.add(Status.ENABLE.name());
        merchantBankCriteria.setStatusList(list);
        PageHelper.startPage(merchantBankCriteria.getPageNum(), merchantBankCriteria.getPageSize());
        List<MerchantBank> bankList = merchantBankService.selectList(merchantBankCriteria);
        PageInfo<MerchantBank> pageInfo = new PageInfo<>(bankList);
        for (MerchantBank bank : pageInfo.getList()) {
            bank.setAccountName(SensitiveDataUtil.accountNameDesensitization(bank.getAccountName()));
            if (bank.getSettleType() == 0) {
                bank.setCardNo(SensitiveDataUtil.cardNoDesensitization(bank.getCardNo()));
            } else if (bank.getSettleType() == 1) {
                bank.setCardNo(SensitiveDataUtil.alipayAccountNoDesensitization(bank.getCardNo()));
            }
        }
        PageDTO dto = BeanConvertUtils.convert(pageInfo, PageDTO.class);
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "查询结算银行卡变更记录成功", dto);
    }


    @PostMapping("/checkMerchantSettleBank")
    public CommonResult checkMerchantSettleBank(@RequestBody MerchantBankCriteria merchantBankCriteria, MerchantDTO merchantDTO) {
        MerchantBank merchantBank = new MerchantBank();
        merchantBank.setStatus(Status.ENABLE);
        merchantBank.setMerchantId(merchantDTO.getId());
        MerchantBank bank = merchantBankService.selectOne(merchantBank);
        if (bank != null){
            if (!merchantBankCriteria.getAccountName().equals(bank.getAccountName())){
                return new CommonResult(CommonConstants.RETURN_ERROR, "原结算信息收款人姓名不正确");
            }
            if (!merchantBankCriteria.getCardNo().equals(bank.getCardNo())){
                return new CommonResult(CommonConstants.RETURN_ERROR, "原结算信息收款账户不正确");
            }
        }
        return new CommonResult(CommonConstants.RETURN_SUCCESS, "检查成功");
    }
}
